package wml.switcher.view;

import java.io.IOException;
import java.util.ArrayList;

import wml.switcher.ViewSwitcherActivity;
import wml.switcher.command.ActionCommand;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Path;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;

public class ViewController extends BaseAdapter implements
		OnItemSelectedListener {

	private ArrayList<ArrayList<Path>> paths = new ArrayList<ArrayList<Path>>();
	private ViewSwitcherActivity activity;
	private ImageManager images;
	private static int currentPosition;

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			int o = msg.what - currentPosition;
			if (o < 7 || o > -3)
				ViewController.this.notifyDataSetChanged();
			super.handleMessage(msg);
		}
	};

	public ViewController(Context context, String baseUrl) throws IOException {
		this.images = new ImageManager(this.mHandler, baseUrl);
		this.activity = (ViewSwitcherActivity) context;
		for (int i = 0; i < images.getNum(); i++)
			this.paths.add(new ArrayList<Path>());
	}

	public int getCount() {
		return images.getNum();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView view = new ImageView(activity);
		Bitmap bmp = this.images.getImage(position, true);
		view.setImageBitmap(bmp);
		view.setAdjustViewBounds(true);
		view.setLayoutParams(new Gallery.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		return view;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		currentPosition = position;
		Bitmap bmp = this.images.getImage(position, false);
		this.activity.getCommandSender().sendCommand(
				new ActionCommand(ActionCommand.ACTION_JMP, currentPosition));
		if (bmp == null)
			return;
		this.activity.getMainView().setView(
				bmp.copy(Bitmap.Config.ARGB_8888, true), this.getPath());
		bmp.recycle();
		System.gc();
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
	}

	public Path getPath() {
		ArrayList<Path> paths = this.paths.get(currentPosition);
		if (paths == null) {
			paths = new ArrayList<Path>();
			this.paths.set(currentPosition, paths);
		}
		Path path = new Path();
		for (Path tp : paths) {
			path.addPath(tp);
		}
		return path;
	}

	public void addPath(Path path) {
		this.paths.get(currentPosition).add(path);
	}

	public void removePrePath() {
		ArrayList<Path> paths = this.paths.get(currentPosition);
		if (paths.size() > 0) {
			paths.remove(paths.size() - 1);
		}
		this.onItemSelected(null, null, currentPosition, 0);
	}

	public void removeAllPaths() {
		this.paths.get(currentPosition).clear();
		this.onItemSelected(null, null, currentPosition, 0);
	}

	public static int getCurrentPosition() {
		return currentPosition;
	}
}